﻿namespace IPMS.BLL
{
    using IPMS.DAL;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class Agent
    {
        private string _Address;
        private string _Agent;
        private string _ChineseName;
        private string _ContactPerson;
        private string _ContactPerson2;
        private string _EmailAddress;
        private string _EmailAddress2;
        private string _Fax;
        private string _Fax2;
        private string _JobTitle;
        private string _MobilePhone;
        private string _MobilePhone2;
        private string _Name;
        private string _Phone;
        private string _Phone2;
        private string _WebSite;
        private bool _IsDelete;

        public Agent(string agent)
        {
            this._Agent = agent;
        }

        public Agent(string agent, string name, string chineseName, string address, string contactPerson, string jobTitle, string phone, string fax)
        {
            this._Agent = agent;
            this._Name = name;
            this._ChineseName = chineseName;
            this._Address = address;
            this._ContactPerson = contactPerson;
            this._JobTitle = jobTitle;
            this._Phone = phone;
            this._Fax = fax;
        }

        public int GetAgentDetails()
        {
            SqlCommand sqlCmd = new SqlCommand();
            DataAccessLayer DAL = new DataAccessLayer();
            DAL.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@Agent", SqlDbType.NVarChar, 0xff, ParameterDirection.Input, this._Agent.ToString());
            DAL.AddParamToSQLCmd(sqlCmd, "@Name", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@ChineseName", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@Address", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@ContactPerson", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@JobTitle", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@Phone", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@Fax", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@Phone2", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@MobilePhone", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@MobilePhone2", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@WebSite", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@EmailAddress", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@EmailAddress2", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@ContactPerson2", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@Fax2", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@IsDelete", SqlDbType.Bit, 1, ParameterDirection.Output, null);
            DAL.SetCommandType(sqlCmd, CommandType.StoredProcedure, "GetAgentDetailsSp");
            DAL.ExecuteScalarCmd(sqlCmd);
            int resultValue = (int) sqlCmd.Parameters["@ReturnValue"].Value;
            this.Agent_Name = IPDataFormat.ValidString(sqlCmd.Parameters["@Name"].Value.ToString());
            this.Agent_ChineseName = IPDataFormat.ValidString(sqlCmd.Parameters["@ChineseName"].Value.ToString());
            this.Agent_Address = IPDataFormat.ValidString(sqlCmd.Parameters["@Address"].Value.ToString());
            this.Agent_ContactPerson = IPDataFormat.ValidString(sqlCmd.Parameters["@ContactPerson"].Value.ToString());
            this.Agent_JobTitle = IPDataFormat.ValidString(sqlCmd.Parameters["@JobTitle"].Value.ToString());
            this.Agent_Phone = IPDataFormat.ValidString(sqlCmd.Parameters["@Phone"].Value.ToString());
            this.Agent_Fax = IPDataFormat.ValidString(sqlCmd.Parameters["@Fax"].Value.ToString());
            this.Agent_Phone2 = IPDataFormat.ValidString(sqlCmd.Parameters["@Phone2"].Value.ToString());
            this.Agent_MobilePhone = IPDataFormat.ValidString(sqlCmd.Parameters["@MobilePhone"].Value.ToString());
            this.Agent_MobilePhone2 = IPDataFormat.ValidString(sqlCmd.Parameters["@MobilePhone2"].Value.ToString());
            this.Agent_WebSite = IPDataFormat.ValidString(sqlCmd.Parameters["@WebSite"].Value.ToString());
            this.Agent_EmailAddress = IPDataFormat.ValidString(sqlCmd.Parameters["@EmailAddress"].Value.ToString());
            this.Agent_EmailAddress2 = IPDataFormat.ValidString(sqlCmd.Parameters["@EmailAddress2"].Value.ToString());
            this.Agent_ContactPerson2 = IPDataFormat.ValidString(sqlCmd.Parameters["@ContactPerson2"].Value.ToString());
            this.Agent_Fax2 = IPDataFormat.ValidString(sqlCmd.Parameters["@Fax2"].Value.ToString());
            this._IsDelete = Convert.ToBoolean(sqlCmd.Parameters["@IsDelete"].Value);
            
            return resultValue;
        }

        public bool Update(bool IsDelete, string UpdateBy, string UpdateIP, string agent)
        {
            bool isSuccess = false;
            try
            {
                SqlCommand sqlCommand = new SqlCommand();
                DataAccessLayer DAL = new DataAccessLayer();

                string sqlString = "UPDATE [Agent] SET IsDelete = @IsDelete,UpdateBy = @UpdateBy,UpdateDate = GETDATE(), UpdateIP = @UpdateIP WHERE (([Agent] = @Agent)) ";

                DAL.SetCommandType(sqlCommand, CommandType.Text, sqlString);
                DAL.AddParamToSQLCmd(sqlCommand, "@IsDelete", SqlDbType.Bit, 1, ParameterDirection.Input, IsDelete);
                DAL.AddParamToSQLCmd(sqlCommand, "@UpdateBy", SqlDbType.NVarChar, 60, ParameterDirection.Input, UpdateBy);
                DAL.AddParamToSQLCmd(sqlCommand, "@UpdateIP", SqlDbType.NVarChar, 50, ParameterDirection.Input, UpdateIP);
                DAL.AddParamToSQLCmd(sqlCommand, "@Agent", SqlDbType.NVarChar, 60, ParameterDirection.Input, agent);
                DAL.ExecuteScalarCmd(sqlCommand);

                isSuccess = true;
            }
            catch { }

            return isSuccess;
        }

        public string Agent_Address
        {
            get
            {
                return this._Address;
            }
            set
            {
                this._Address = value;
            }
        }

        public string Agent_ChineseName
        {
            get
            {
                return this._ChineseName;
            }
            set
            {
                this._ChineseName = value;
            }
        }

        public string Agent_Code
        {
            get
            {
                return this._Agent;
            }
            set
            {
                this._Agent = value;
            }
        }

        public string Agent_ContactPerson
        {
            get
            {
                return this._ContactPerson;
            }
            set
            {
                this._ContactPerson = value;
            }
        }

        public string Agent_ContactPerson2
        {
            get
            {
                return this._ContactPerson2;
            }
            set
            {
                this._ContactPerson2 = value;
            }
        }

        public string Agent_EmailAddress
        {
            get
            {
                return this._EmailAddress;
            }
            set
            {
                this._EmailAddress = value;
            }
        }

        public string Agent_EmailAddress2
        {
            get
            {
                return this._EmailAddress2;
            }
            set
            {
                this._EmailAddress2 = value;
            }
        }

        public string Agent_Fax
        {
            get
            {
                return this._Fax;
            }
            set
            {
                this._Fax = value;
            }
        }

        public string Agent_Fax2
        {
            get
            {
                return this._Fax2;
            }
            set
            {
                this._Fax2 = value;
            }
        }

        public string Agent_JobTitle
        {
            get
            {
                return this._JobTitle;
            }
            set
            {
                this._JobTitle = value;
            }
        }

        public string Agent_MobilePhone
        {
            get
            {
                return this._MobilePhone;
            }
            set
            {
                this._MobilePhone = value;
            }
        }

        public string Agent_MobilePhone2
        {
            get
            {
                return this._MobilePhone2;
            }
            set
            {
                this._MobilePhone2 = value;
            }
        }

        public string Agent_Name
        {
            get
            {
                return this._Name;
            }
            set
            {
                this._Name = value;
            }
        }

        public string Agent_Phone
        {
            get
            {
                return this._Phone;
            }
            set
            {
                this._Phone = value;
            }
        }

        public string Agent_Phone2
        {
            get
            {
                return this._Phone2;
            }
            set
            {
                this._Phone2 = value;
            }
        }

        public string Agent_WebSite
        {
            get
            {
                return this._WebSite;
            }
            set
            {
                this._WebSite = value;
            }
        }

        public bool IsDelete
        {
            get
            {
                return this._IsDelete;
            }
            set
            {
                this._IsDelete = value;
            }
        }
    }
}

